// packageF/Throughpay/Throughpay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    activeOne: 0,
    listData: [],
    info: [],
    active: 0,
    activeOne: 0,
    listData: [],
    user_assets: 0,
    show:false,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '钱包',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  resrtTap(e) {
    console.log(e)
    this.init();
    this.setData({
      active: e.detail.index
    })
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      listData: []
    })
  },
  getInfo() {
    let urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.wallet.index');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            info: res.data.tabs,
            show:true
          })
          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let json = {
      // value: this.data.info[this.data.active == 0 ? "currency_1" : "currency_2"].value,
      value:this.data.active==1?1:(this.data.active==2)?2:0,
      page: this.data.page
    };
    let urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.wallet.getValueData');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.records.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.records.data,
            user_assets: res.data.user_assets
          })
          if(this.data.listData && this.data.listData.length>0){
            this.data.listData.forEach(item =>{
              item.format_time = this.timestamp(item.create_at)
            })
            this.setData({
              listData:this.data.listData
            })
          }

        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  timestamp(time){
   var _time = '';
   if(time<1000000000000){
     _time = time*1000
   }else{
     _time = time
   }
   var date= new Date(_time);
    var years = date.getFullYear();
    var month= this.supplement(date.getMonth()+1);
    var day= this.supplement(date.getDate());
    var hour= this.supplement(date.getHours());
    var minutes = this.supplement(date.getMinutes());
    return years+'.'+month+'.'+day+'  '+hour+':'+minutes;
  },
  supplement(number){
    return number<10?'0'+number:number
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let json = {
        value: this.data.info[this.data.active == 0 ? "currency_1" : "currency_2"].value,
        page: this.data.page
      };
      urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.wallet.getValueData');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.records.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  pay(e){
    let data = e.currentTarget.dataset.info;
    if(data.value==0){
      wx.navigateTo({url: '/packageB/member/love/loveWithdrawals/loveWithdrawals'})
    }else{
      wx.navigateTo({
        url: '/packageF/love_transform/love_transform?value='+data.value,
      })
    }
  }
})